package com.zr.bookcorner.activity;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.CompoundButton;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.zh.baselibrary.base.BaseActivity;
import com.zh.baselibrary.constant.ExtraName;
import com.zh.baselibrary.utils.ClickUtils;
import com.zh.baselibrary.utils.IntentUtils;
import com.zh.baselibrary.utils.TTSUtils;
import com.zh.baselibrary.views.ToastView;
import com.zr.andealib.util.HardEvent;
import com.zr.andealib.util.constant.AndeaConstant;
import com.zr.andealib.util.model.InventoryReport;
import com.zr.bookcorner.R;
import com.zr.bookcorner.dialog.ManagerVerificationDialog;
import com.zr.bookcorner.fragment.BookSelectFragment;
import com.zr.bookcorner.fragment.BorrowReturnFragment;
import com.zr.bookcorner.presenter.ChoosePresenter;
import com.zr.bookcorner.presenter.view.ChooseView;
import com.zr.bookcorner.service.VoiceEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Arrays;
import java.util.List;

import butterknife.OnCheckedChanged;

public class CirCulateActivity extends BaseActivity<ChoosePresenter> implements ChooseView, View.OnClickListener {


    private int boxNum;

    private FragmentManager mFragmentManager = getSupportFragmentManager();
    private BorrowReturnFragment borrowReturnFragment;
    private BookSelectFragment bookSelectFragment;
    private List<String> tagList = Arrays.asList("borrowReturnTag", "bookSelectTag");
    private final int TAB_BORROW = 0, TAB_SELECT = 1;

    private ManagerVerificationDialog mVerificationDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.cir_culate_activity;
    }

    @Override
    protected void init() {
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        borrowReturnFragment = new BorrowReturnFragment();
        transaction.add(R.id.content, borrowReturnFragment, tagList.get(TAB_BORROW));
        bookSelectFragment = new BookSelectFragment();
        transaction.add(R.id.content, bookSelectFragment, tagList.get(TAB_SELECT));
        transaction.commitAllowingStateLoss();
        setTabSelection(TAB_BORROW);
        mPresenter.setType(0);
    }

    @Override
    protected ChoosePresenter createPresenter() {
        return new ChoosePresenter();
    }


    @Override
    public void onClick(View v) {
        if (ClickUtils.isFastClick()) {
            boxNum = Integer.parseInt((String) v.getTag());
            mPresenter.checkDoorStatus(boxNum);
        }
    }

    @OnCheckedChanged({R.id.radio_borrow, R.id.radio_select})
    public void onCheckedChanged(CompoundButton view, boolean isChanged) {
        if (isChanged) {
            switch (view.getId()) {
                case R.id.radio_borrow:
                    setTabSelection(TAB_BORROW);
                    break;
                case R.id.radio_select:
                    setTabSelection(TAB_SELECT);
                    break;
            }
        }
    }

    @Override
    public void showDialog() {
        mVerificationDialog = new ManagerVerificationDialog(this, R.style.DialogTheme, null);
        mVerificationDialog.setShowQRCode(false);
        mVerificationDialog.show();
    }

    @Override
    public void openSuccess() {
        dismissDialog();
        Bundle bundle = new Bundle();
        bundle.putInt(ExtraName.KEY_DATA, boxNum);
        IntentUtils.instance().startActivity(this, BorrowReturnDetailActivity.class, bundle);
      //  TTSUtils.getInstance().speak(getString(R.string.open_success));
        EventBus.getDefault().post(new VoiceEvent(getString(R.string.open_success)));
    }

    @Override
    public void openError() {
        //TTSUtils.getInstance().speak(getString(R.string.open_error));
        EventBus.getDefault().post(new VoiceEvent(getString(R.string.open_error)));
        dismissDialog();
    }

    @Override
    public void defyError() {
        //TTSUtils.getInstance().speak(getString(R.string.defy_error));
        EventBus.getDefault().post(new VoiceEvent(getString(R.string.defy_error)));
        dismissDialog();
    }

    private void dismissDialog() {
        if (mVerificationDialog != null && mVerificationDialog.isShowing())
            mVerificationDialog.cancel();
    }


    /**
     * 根据传入的index参数来设置选中的tab页。
     */
    private void setTabSelection(int index) {
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        hideAllFragments(transaction);
        switch (index) {
            case TAB_BORROW:
                if (borrowReturnFragment == null) {
                    borrowReturnFragment = new BorrowReturnFragment();
                    transaction.add(R.id.content, bookSelectFragment);
                } else {
                    transaction.show(borrowReturnFragment);
                }
                if (bookSelectFragment != null) {
                    //关闭软键盘
                    bookSelectFragment.hideSoftInput();
                }
                break;
            case TAB_SELECT:
                if (bookSelectFragment == null) {
                    bookSelectFragment = new BookSelectFragment();
                    transaction.add(R.id.content, bookSelectFragment);
                } else {
                    transaction.show(bookSelectFragment);
                }
                mPresenter.count();
                break;
        }
        transaction.commit();

    }

    @Override
    public void onHint(String hint) {
        ToastView.showShort(hint);
        dismissDialog();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.initLock();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mPresenter.onDestroy();
    }


    @Override
    public void onSuccess(Object object) {
        dismissDialog();
    }

    /**
     * 将所有的Fragment都置为隐藏状态。
     *
     * @param transaction 用于对Fragment执行操作的事务
     */
    public void hideAllFragments(FragmentTransaction transaction) {
        if (null != borrowReturnFragment) {
            transaction.hide(borrowReturnFragment);
        }
        if (null != bookSelectFragment) {
            transaction.hide(bookSelectFragment);
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (bookSelectFragment != null) {
            //关闭软键盘
            bookSelectFragment.hideSoftInput();
        }
        return super.onTouchEvent(event);
    }
}
